package com.deluxelx.soul.inn.service.common;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deluxelx.soul.inn.bean.relation.FollowRelation;
import com.deluxelx.soul.inn.common.param.CollectedParam;
import com.deluxelx.soul.inn.common.util.RedisUtil;
import com.deluxelx.soul.inn.mapper.common.FollowRelationSupport;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.deluxelx.soul.inn.common.constant.ArticleConstant.ARTICLE_CACHE_KEY_PREFIX;
import static com.deluxelx.soul.inn.common.constant.ArticleConstant.ARTICLE_FOLLOW_KEY;
import static com.deluxelx.soul.inn.common.constant.CounselorConstant.COUNSELOR_CACHE_KEY_PREFIX;
import static com.deluxelx.soul.inn.common.constant.CounselorConstant.COUNSELOR_FOLLOW_KEY;
import static com.deluxelx.soul.inn.common.constant.CourseConstant.COURSE_CACHE_KEY_PREFIX;
import static com.deluxelx.soul.inn.common.constant.CourseConstant.COURSE_FOLLOW_KEY;
import static com.deluxelx.soul.inn.common.constant.QuestionConstant.QUESTION_CACHE_KEY_PREFIX;
import static com.deluxelx.soul.inn.common.constant.QuestionConstant.QUESTION_FOLLOW_KEY;
import static com.deluxelx.soul.inn.common.constant.UserConstant.USER_CACHE_KEY_PREFIX;

/**
 * 关注、收藏等通用功能服务工具
 *
 * @author liaozihao
 * @date 2024-04-24 9:51
 */
@Component
public class FollowRelationService extends ServiceImpl<FollowRelationSupport, FollowRelation> {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private FollowRelationSupport followRelationSupport;

    public List<String> getMyFollowIds(String userId, String type) {
        if (StrUtil.isBlank(userId)) {
            return Collections.emptyList();
        }
        String key = USER_CACHE_KEY_PREFIX + userId + StrUtil.COLON;
        switch (type) {
            case ARTICLE_FOLLOW_KEY:
                key += ARTICLE_CACHE_KEY_PREFIX;
                break;
            case COUNSELOR_FOLLOW_KEY:
                key += COUNSELOR_CACHE_KEY_PREFIX;
                break;
            case COURSE_FOLLOW_KEY:
                key += COURSE_CACHE_KEY_PREFIX;
                break;
            case QUESTION_FOLLOW_KEY:
                key += QUESTION_CACHE_KEY_PREFIX;
                break;
        }
        List<String> keys = redisUtil.scanKeys(key + "*");

        List<String> ids = followRelationSupport.queryMyFollow(userId, type);
        Set<String> idSet = new HashSet<>(ids);
        for (String keyItem : keys) {
            String[] split = keyItem.split(StrUtil.COLON);
            String id = split[1].substring(split[1].indexOf(StrUtil.C_UNDERLINE) + 1);
            Integer flag = redisUtil.get(keyItem, Integer.class);
            if (flag == 1) {
                idSet.add(id);
            } else if (flag == 0) {
                idSet.remove(id);
            }
        }
        return new ArrayList<>(idSet);
    }

    public Integer checkFollow(CollectedParam collectedParam) {
        return followRelationSupport.checkFollow(collectedParam);
    }

}
